package com.example.private_auto_install.controller;

import com.example.private_auto_install.ftpGIt.Ftp;
import com.example.private_auto_install.ftpGIt.MailUtil;
import com.example.private_auto_install.ftpGIt.UtilMonitor;
import com.example.private_auto_install.tool.HandleLinux;
import com.example.private_auto_install.toolby.ConsoleMain;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@RestController
@RequestMapping("/base")
@CrossOrigin
public class MainController {

    @GetMapping("init")
    public Map<String, String> init(String IP, String type) {
        Ftp ftp = new Ftp(IP);
        //这个执行完了
        String code = "error";
        String outDeletePath = "/home/privateCode";
        String path = outDeletePath;
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("code", "99");

        //依据type来执行不同的sh。
        String targetSh = "installSJ.sh";
        String twoSh = "0";
        String name = "";
        switch (type) {
            case "SJ":
                targetSh = "installSJ.sh";
                name = "苏教版";
                break;
            case "JK":
                targetSh = "installJK.sh";
                name = "教科版";
                break;
            case "JPCB":
                targetSh = "installJPCB.sh";
                name = "初中理化生";
                twoSh = "1";
                break;
            case "JP":
                targetSh = "installJPCB.sh";
                name = "初中物理";
                twoSh = "2";
                break;
            case "JC":
                targetSh = "installJPCB.sh";
                name = "初中化学";
                twoSh = "3";
                break;
            case "JB":
                targetSh = "installJPCB.sh";
                name = "初中生物";
                twoSh = "4";
                break;
            case "HP":
                targetSh = "installJPCB.sh";
                name = "高中物理";
                twoSh = "6";
                break;
            case "HC":
                targetSh = "installJPCB.sh";
                name = "高中化学";
                twoSh = "7";
                break;
            case "HB":
                targetSh = "installJPCB.sh";
                name = "高中生物";
                twoSh = "8";
                break;
            case "HPCB":
                targetSh = "installJPCB.sh";
                name = "高中理化生";
                twoSh = "5";
                break;
        }

        System.out.println("*********** 开始下载脚本 " + name + "************");
        //首先执行  连接linux
        Map<String, String> map = new HashMap<>();
        map.put("host", IP);
        List<String> list = null;

        /** you 有问题。注释
         //然后让linux执行远程sh文件。下载压缩包，并安装        无法直接使用当前命令。会下载到一半停止
         //        map.put("command", "curl -O http://192.168.10.200:7777/privateTar/" + targetSh + " && chmod +777  ./" + targetSh + " && ./" + targetSh + " && rm -rf ./" + targetSh);
         //        map.put("command", "curl -O http://192.168.10.200:7777/privateTar/" + targetSh + " && chmod +777  ./" + targetSh);
         //        list = HandleLinux.exeResult(map);
         //
         //        System.out.println("执行下载的命令");
         //        map.put("command", " ./" + targetSh);
         //        list = HandleLinux.exeResult(map);


         // 更换下思路。这里不要下载。改为上传。
         //        System.out.println("下载文件");
         //        map.put("command", " curl -o /home/update.zip \"http://192.168.10.200:7777/privateTar/fzinstallSJ-1004L.zip\"");
         //        list = HandleLinux.exeResult(map);
         */


        File file = new File("/home/home/filesDownload/privateTar/fzinstallSJ-1004L.zip");
//        File file =new File("G:\\test11.txt");

        boolean flag1 = ftp.singleUpload("/home/", outDeletePath, file, "update.zip");
        if (flag1) {
            System.out.println("********** 安装文件 传输完成 ************");
        } else {
            System.out.println("********** 安装文件 传输失败 ************");
        }

        System.out.println("解压缩zip文件");
        map.put("command", "unzip -o /home/update.zip -d /home");
        list = HandleLinux.exeResult(map);

        System.out.println("授权所有的sh");
        map.put("command", "find /home/fzinstall -type f -name \"*.sh\"  -exec chmod 777 {} \\;");
        list = HandleLinux.exeResult(map);


        System.out.println("初始化项目");
        map.put("command", "sh /home/fzinstall/install.sh");
        list = HandleLinux.exeResult(map);


        // 执行完成后启动java
        map.put("command", "java -jar /home/fzinstall/jar/teaching-both-scienceExperiment-1.0.0.jar");
        list = HandleLinux.exeResult(map);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).indexOf("本机的机器码为") >= 0) {
                String[] split = list.get(i).split("：");
                if (split.length > 1) {
                    code = split[1].trim();
                    break;
                } else {
                    System.out.println("未找到机器码");
                }
            }
        }

        try {
            System.out.println("********** 机器码为： " + code + "************");
            if (!"0".equals(twoSh)) {
                map.put("command", "curl -O http://192.168.10.200:7777/privateTar/changeProject.sh && chmod +777  ./changeProject.sh && ./changeProject.sh " + twoSh + "  && rm -rf ./changeProject.sh");
                list = HandleLinux.exeResult(map);
            }
            // 用工具生成文件
            ConsoleMain.createFile(code, path);

            // 传输 path下的路径传输到不同ip上，
            System.out.println("********** 开始构建授权文件： " + code + "************");

            File file1 = new File(path + File.separator + code + ".lic");
            boolean flag = ftp.singleUpload("/home/fzinstall/jar/", "", file1, file1.getName());

            if (flag) {
                System.out.println("********** 授权文件 传输完成 ************");
            } else {
                System.out.println("********** 授权文件 传输失败 ************");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("下载解压过程中失败了！");
        }
        //最后给，发送ip地址和机器码
        try {
            Properties ftpConfig = UtilMonitor.getAllProperties("ftp");
            MailUtil.sendMsg(ftpConfig.getProperty("name1"), IP + name + "-安装成功", "IP");
            MailUtil.sendMsg(ftpConfig.getProperty("name2"), IP + name + "-安装成功", "IP");
            System.out.println("发送成功！请查看邮箱");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送失败！");
        }

        return resultMap;
    }

}
